package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.db.domain.MarketAddress;
import com.cskaoyan.market.db.domain.MarketAddressExample;
import com.cskaoyan.market.db.domain.MarketUser;
import com.cskaoyan.market.db.mapper.MarketAddressMapper;
import com.cskaoyan.market.service.AdminAddressService;
import com.cskaoyan.market.util.MybatisUtil;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class AdminAddressServiceImpl implements AdminAddressService {
    @Override
    public List<MarketAddress> list(String page, String limit, String sort, String order, String name, String userId) {
        SqlSession session = MybatisUtil.getSession();
        MarketAddressMapper mapper = session.getMapper(MarketAddressMapper.class);
        MarketAddressExample marketAddressExample = new MarketAddressExample();
        MarketAddressExample.Criteria criteria = marketAddressExample.createCriteria();
        if(!StringUtils.isEmpty(name))
            criteria.andNameEqualTo("%" + name + "%");
        if(!StringUtils.isEmpty(userId)) {
            criteria.andUserIdEqualTo(Integer.parseInt(userId));
        }
        marketAddressExample.setOrderByClause(sort + " " + order);
        PageHelper.startPage(Integer.parseInt(page),Integer.parseInt(limit));
        List<MarketAddress> marketAddresses = mapper.selectByExample(marketAddressExample);
        session.commit();
        session.close();
        return marketAddresses;
    }
}
